package listbyorder.access101_200.test160;

import listbyorder.utils.ListNode;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/13 13:07
 */
public class Solution1 {

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) return null;
        int size = 0;
        ListNode tempA = headA;
        ListNode tempB = headB;
        while (tempA.next != null) {
            size++;
            tempA = tempA.next;
        }
        while (tempB.next != null) {
            size--;
            tempB = tempB.next;
        }
        if (tempA != tempB) return null;
        tempA = size > 0 ? headA : headB;
        tempB = tempA == headA ? headB : headA;
        size = Math.abs(size);
        while (size != 0) {
            size--;
            tempA = tempA.next;
        }
        while (tempA != tempB) {
            tempA = tempA.next;
            tempB = tempB.next;
        }
        return tempA;
    }
}
